import java.util.ArrayList;
import java.util.Collection;

@SuppressWarnings("UnusedDeclaration")
public class PointSET {

    private SET<Point2D> points;

    // construct an empty set of points
    public PointSET() {
        this.points = new SET<Point2D>();
    }

    // is the set empty?
    public boolean isEmpty() {
        return this.points.isEmpty();
    }

    // number of points in the set
    public int size() {
        return this.points.size();
    }

    // add the point p to the set (if it is not already in the set)
    public void insert(Point2D p) {
        this.points.add(p);
    }

    // does the set contain the point p?
    public boolean contains(Point2D p) {
        return this.points.contains(p);
    }

    // draw all of the points to standard draw
    public void draw() {
        for (Point2D p : this.points) {
            p.draw();
        }
    }

    // all points in the set that are inside the rectangle
    public Iterable<Point2D> range(RectHV rect) {
        Collection<Point2D> result = new ArrayList<Point2D>();
        for (Point2D p : this.points) {
            if (rect.contains(p)) result.add(p);
        }
        return result;
    }

    // a nearest neighbor in the set to p; null if set is empty
    public Point2D nearest(Point2D p) {
        Point2D nearest = null;
        double nearestDistanceSquared = Double.POSITIVE_INFINITY;
        for (Point2D q : this.points) {
            final double distanceSquared = p.distanceSquaredTo(q);
            if (distanceSquared < nearestDistanceSquared) {
                nearestDistanceSquared = distanceSquared;
                nearest = q;
            }
        }
        return nearest;
    }

}
